package com.bnzj.cloud.business.core.service;

import com.bnzj.cloud.business.core.persistence.entity.Group;
import com.bnzj.cloud.business.core.persistence.entity.SysSetting;
import com.bnzj.cloud.business.core.persistence.entity.SysUser;
import com.bnzj.cloud.business.core.persistence.repository.SysSettingRepository;
import com.bnzj.core.exception.BusinessException;
import com.bnzj.core.persistence.BaseService;
import com.bnzj.core.persistence.query.CompareExpression;
import com.bnzj.core.persistence.query.CompareType;
import com.bnzj.core.persistence.query.IExpression;
import com.bnzj.core.persistence.query.QueryHelpBean;
import com.bnzj.core.rest.ResultCode;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * @author barry
 * @date 2019-10-15
 */
@Service
public class SysSettingService extends BaseService<SysSettingRepository, SysSetting, Long> {

    /**
     * @return
     */
    public static QueryHelpBean getQueryHelpBean() {
        String selectSQL = "select *";
        String whereSQL = " from bnz_sys_setting where 1=1 ";
        QueryHelpBean queryBean = new QueryHelpBean(selectSQL + whereSQL, "select count(1)" + whereSQL, SysSetting.class);
        return queryBean;
    }

    /**
     * @return
     */
    public static QueryHelpBean getGroupHelpBean() {
        String selectSQL = "select *";
        String whereSQL = " from bnz_group where 1=1 ";
        QueryHelpBean queryBean = new QueryHelpBean(selectSQL + whereSQL, "select count(1)" + whereSQL, Group.class);
        return queryBean;
    }

    public static QueryHelpBean findAllByGroupId() {
        String selectSQL = "select *";
        String whereSQL = " from bnz_sys_user where 1=1 ";
        QueryHelpBean queryBean = new QueryHelpBean(selectSQL + whereSQL, "select count(1)" + whereSQL, SysUser.class);
        return queryBean;
    }


    public Page<?> queryList(HttpServletRequest request, Pageable pageable) {
        QueryHelpBean bean = SysSettingService.getQueryHelpBean();
        bean.setPageable(pageable);
        String keyWord = request.getParameter("keyWord");
        if (ObjectUtils.isNotEmpty(keyWord)) {
            IExpression orCondition1 = CompareExpression.build("bnz_sys_setting.name", CompareType.like, keyWord);
            bean.addWhere(orCondition1);
        }
        return findAll(bean);
    }


    public SysSetting getSysSettingByKey(String key) {
        SysSetting sysSetting = new SysSetting();
        sysSetting.setKey(key);
        Example<SysSetting> condition = Example.of(sysSetting);
        Optional<SysSetting> one = this.repository.findOne(condition);
        if (!one.isPresent()) {
            throw new BusinessException("找不到该系统配置！", "" + ResultCode.getResultCode(ResultCode.RESOURCE_NOT_EXIST));
        }
        return one.get();
    }
}
